package com.qire.manhua.model.viewModel.impl;

import com.qire.antscore.annotation.PropertyObserve;
import com.qire.common.basic.StringUtil;
import com.qire.common.collect.ArrayUtil;
import com.qire.common.support.base.BaseViewModel;
import com.qire.common.support.base.IViewModel;
import com.qire.manhua.BR;
import com.qire.manhua.R;
import com.qire.manhua.dal.service.TeensModeService;
import com.qire.manhua.model.enumeration.TeensModeScene;

import androidx.lifecycle.MutableLiveData;

public class TeensLockTimeoutViewModel extends BaseViewModel implements IViewModel {

    private enum PanelSate {

        VerifyPanel("立即验证");

        final String[] passwordCodes = new String[4];
        final String nextBtnText;

        PanelSate(String nextBtnText) {
            this.nextBtnText = nextBtnText;
        }

        public boolean isFull() {
            for(String code : passwordCodes) {
                if(StringUtil.isEmpty(code)) {
                    return false;
                }
            }
            return true;
        }

        public String passwordCode() {
            StringBuilder passwordCodeBuilder = new StringBuilder(4);
            for(String code : passwordCodes) {
                if(StringUtil.notEmpty(code)) {
                    passwordCodeBuilder.append(code.charAt(0));
                }
            }
            return passwordCodeBuilder.toString();
        }
    }

    private PanelSate panelState = PanelSate.VerifyPanel;

    @Override
    public int getVariableId() {
        return BR.teensLockTimeoutViewModel;
    }

    //region    #附加管理 begin
    /**
     * 附加属性绑定
     */
    public void onAddition() {
        refreshUI();
    }

    /**
     * 清理函数，在Activity退出时，ViewModel生命周期到期会被回收，调用该事件，
     * 页面由于切换、横竖屏转换、内存回收等情况并不会影响到ViewModel的生命周期。
     */
    @Override
    protected void onCleared() {
        super.onCleared();
    }
    //endregion #附加管理 end

    // region    #业务

    public void verifyPassword() {
        new TeensModeService().verifyPassword(panelState.passwordCode(), TeensModeScene.TimeOut.sceneLabel);
    }

    // endregion #业务

    // region    #UI操作

    private void refreshUI() {
        set("nextBtnText", panelState.nextBtnText);
        refreshNextBtnBgRid(panelState.isFull());
    }

    private void refreshNextBtnBgRid(boolean clickable) {
        set("nextBtnClickable", clickable);
        set("nextBtnBgRid", clickable ? R.drawable.app_teens_button_yellow : R.drawable.app_teens_button_gray);
    }

    private void resetPasswordCodeUI() {
        set("passwordCode", new CharSequence[] {"", "", "", ""});
        set("codeFocusIndex", 0);
    }

    @PropertyObserve(name = "passwordCode")
    public void onPasswordCodeChange(CharSequence[] passwordCode) {
        ArrayUtil.replace(passwordCode, panelState.passwordCodes);
        refreshNextBtnBgRid(panelState.isFull());
    }

    // endregion #UI操作

    // region    #UI绑定

    public MutableLiveData<CharSequence[]> getPasswordCode() {
        return get("passwordCode");
    }
    public MutableLiveData<Integer> getCodeFocusIndex() {
        return get("codeFocusIndex");
    }

    public MutableLiveData<Boolean> getNextBtnClickable() {
        return get("nextBtnClickable");
    }

    public MutableLiveData<CharSequence> getNextBtnText() {
        return get("nextBtnText");
    }

    public MutableLiveData<Integer> getNextBtnBgRid() {
        return get("nextBtnBgRid");
    }

    // endregion #UI绑定
}
